import tkinter
import Operation
from tkinter.filedialog import *
from PIL import Image,ImageTk
import json

#  开头
def Action(win):
    font_show = ('楷体', 20)
    label_show = tkinter.Label(win, text='Welcome to 学生信息操作系统！', font=font_show)
    label_show.place(x=200,y = 30)

#  菜单提示栏
def menu_main(win):

    # 菜单提示栏
    menu_main = tkinter.Menu(win)

    # list_operation = ['select-<1>', 'insert-<3>', 'remove-<4>', 'change-<5>']

    # menu_main.add_command(label = 'select-<1>',relief='disabled')
    menu_main.add_command(label = 'select-<1>')

    # menu_main.add_command(label = 'select_all-<2>')






    # menu_find 下拉栏
    # for item in ['select_all-<1>']:
    #     menu_find.add_command(label = item,command = item)
    # menu_main.add_cascade(label = '查看',menu = menu_find)
    # # menu_operation 下拉栏
    # for item in ['change-<2>']:
    #     menu_operation.add_command(label = item)
    # menu_main.add_cascade(label = '编辑',menu = menu_operation)

    win['menu'] = menu_main

    #  -----------  操作流程 -------------

#  指令标签
def label_code(win):
    font_code = ('楷体',15)
    label_code = tkinter.Label(win,text = '请输入指令:',font=font_code )
    label_code.place(x=50,y=90)

# 滚动条列表事件触发
def scroball(reader):
    # 滚动条事件触发
    def print_item(event):
        print(mylist.get(mylist.curselection()))
    root = tkinter.Tk()
    mylist = Listbox(root,font = '楷体',width=80,height=20)
    mylist.bind('<ButtonRelease-1>',print_item)

    for line in reader:
        mylist.insert(END,line)

    mylist.pack()
    scrollbar = Scrollbar(root)
    scrollbar.pack(side = RIGHT,fill=Y)
    scrollbar.config(command = mylist.yview)
    mylist.configure(yscrollcommand = scrollbar.set)
    root.mainloop()


# choose --》 指令1 的 行为标签
def class_button_information(win):
    font_code = ('楷体', 15)
    label_input = tkinter.Label(win, text='请输入学号：', font=font_code)
    label_input.place(x=50, y=350)
    entry_input = tkinter.Entry(win, width=20)
    entry_input.place(x=100, y=380)

    label_input_clas = tkinter.Label(win, text='请输入班级：', font=font_code)
    label_input_clas.place(x=50, y=410)
    entry_input_clas = tkinter.Entry(win, width=20)
    entry_input_clas.place(x=100, y=440)


    def judge1():
        entry_sid = Operation.get_text(entry_input)
        entry_clas = Operation.get_text(entry_input_clas)

        sid_record_information = Operation.Record_students_information()
        clas_record_information = Operation.Sorted_information() # 实例化

        sid_information = sid_record_information.reader_students_information()
        reader = clas_record_information.reader_sorted_information() # 实例方法

        if entry_sid not in sid_information:
            font_tishi = ('楷体', 15)
            label_tishi = tkinter.Label(win, text='无此人员', font=font_tishi, fg='red', width=10)
            label_tishi.place(x=100, y=410)
        else:
            res = clas_record_information.rem_sorted_information(entry_clas, entry_sid)
            if res == 'true':
                scroball(reader[entry_clas])
            else:
                font_code = ('楷体', 15)
                label_input = tkinter.Label(win, text='输入有误', font=font_code,fg = 'red')
                label_input.place(x=100, y=470)


    true_button = tkinter.Button(win, text='确定', command=judge1)
    true_button.place(x=260, y=440)


def choose(entry_code,win):
    get_code = Operation.get_text(entry_code)
    if get_code == '1':
        class_button_information(win)
    else:
        font_code = ('楷体', 15)
        label_input = tkinter.Label(win, text='无此指令及操作!',fg = 'red', font=font_code)
        label_input.place(x=100, y=170)

def opertion():

    window = tkinter.Toplevel()
    window.title(' Students Information !')
    window.geometry('800x600')

    # 开头
    Action(window)

    # image1 = Image.open(r'images\vx12.png')
    # ima = ImageTk.PhotoImage(image1)
    # label_right = tkinter.Label(window, image=ima)
    # label_right.place(x = 400,y=100)

    image1 = PhotoImage(file = r'images\vx12.png')
    label_right = tkinter.Label(window, image=image1)
    label_right.place(x = 400,y=100)


    # 菜单提示栏
    menu_main(window)

    #  -----------  操作流程 -------------

    # 指令标签栏
    label_code(window)

    # entry栏
    entry_code = tkinter.Entry(window,width = 20)
    entry_code.place(x=100,y=120)
    def judge1():
        choose(entry_code,window)

    true_button = tkinter.Button(window,text = '确定',command = judge1)
    true_button.place(x=250,y=120)
    window.mainloop()

